const Router = require('koa-router')
// const fs = require('fs');
const qq = require("./data/qq");
const initData = require('./mysql/init');
const getqq = require('./mysql/getqq');
const setcookie = require('./mysql/setcookie');
module.exports = (app) => {
	const router = new Router()
	router.post('/getqq', async (ctx, next) => {
		try {
			let {num} = ctx.request.body
			var result =await getqq(num);
			if(result.result=='success'){
				var exp = new Date();
				exp.setTime(exp.getTime() + 1);
				ctx.cookies.set(
					'id',result.data[0].id,{
						domain:'10.220.115.14', // 写cookie所在的域名
						path:'10.220.115.14:9000',       // 写cookie所在的路径
						maxAge: 2*60*60*1000,   // cookie有效时长
						expires:exp, // cookie失效时间
						httpOnly:false,  // 是否只用于http请求中获取
					}
				);
				ctx.body = result.data;
			}else{
				ctx.body=result;
			}
			
			
		} catch ({message}) {
			console.log(message)
			ctx.body = {message}
		}
    })
    router.get('/instert', async (ctx, next) => {
		try {
            ctx.body= await initData({qq});
		} catch ({message}) {
			console.log(message)
			ctx.body = {message}
		}
	})
	router.get('/setcookie', async (ctx, next) => {
		try {
			let {cookie} = ctx.request.query;
			let id = ctx.cookies.get('id');
            ctx.body= await setcookie({id,cookie});
		} catch ({message}) {
			console.log(message)
			ctx.body = {message}
		}
	})
	router.post('/file', async (ctx, next) => {
		try {
			let {url,mobile} = ctx.request.body;
            ctx.body= ctx.request.body;
		} catch ({message}) {
			console.log(message)
			ctx.body = {message}
		}
	})
	app.use(router.routes())
    app.use(router.allowedMethods())
	return router
}